<?php
namespace app\api\model;
use app\admin\model\Warehouse;
use traits\model\SoftDelete;

class Product extends Base
{
    use SoftDelete;
    // 设置当前模型的数据库连接
    public function productPrice(){
        return $this->hasMany("ProductPrice","product_id",'id')->field("product_id,type,price,min_number,max_number");
    }
    public function productSku(){
        return $this->hasMany("ProductSku","product_id",'id')->field("product_id,sku,sku_name");
    }
    public function productSpec(){
        return $this->hasMany("ProductSpec","product_id",'id')->field("product_id,spec_id,spec_title");
    }
    public function productSpecItem(){
        return $this->hasMany("ProductSpecItem","product_id",'id')->field("product_id,spec_id,spec_item_id,spec_item_value");
    }
    public function productTradeHall(){
        return $this->hasMany("TradeHall","product_id",'id');
    }

    public function getSkuStock($product_id,$type,$uid=""){
        $sku = [];
        $where = [];
        $where[] =['a.product_id','eq',$product_id];
        if($type == 1){ //线上商家
            $where[] = ['b.warehouse_id','eq',3];
            $field  = "a.id as sku_id,a.sku,a.sku_name,b.stock";
            $sku = (new ProductSku())
                ->alias("a")
                ->join("om_warehouse_general_sku b","a.sku = b.sku and a.product_id = b.product_id","right")
                ->where($where)
                ->field($field)
                ->select();
        }if($type == 2){ //品牌店 自己的仓库和总仓库
            $warehouse_id = (new Warehouse())->findOne([['uid','eq',$uid]],"id");
            $where[] = ['b.warehouse_id','in',[1,$warehouse_id]];
//            $where[] = ['c.warehouse_id','eq',1];

            $field  = "a.id as sku_id,a.sku,a.sku_name,SUM(b.stock) as stock";
            $sku = (new ProductSku())
                ->alias("a")
                ->join("om_warehouse_general_sku b","a.sku = b.sku and a.product_id = b.product_id","left")
//                ->join("om_warehouse_general_sku c","a.sku = c.sku and a.product_id = c.product_id","right")
                ->where($where)
                ->group("a.id")
                ->field($field)
                ->select();
        }if($type == 3){ //贸易商
            $where[] = ['b.warehouse_id','in',[1,2]];//贸易商在两个仓库 一个总仓库 一个中转仓
            $field  = "a.id as sku_id,a.sku,a.sku_name,SUM(b.stock) as stock";
            $sku = (new ProductSku())
                ->alias("a")
                ->join("om_warehouse_general_sku b","a.sku = b.sku and a.product_id = b.product_id","right")
                ->where($where)
                ->group("a.id")
                ->field($field)
                ->select();
        }
        foreach ($sku as &$value){
            $value['number'] = 0;
        }

        return $sku;
    }
    public function getProductStock($product_id,$type,$uid=""){
        $stock = 0;
        $where = [];
        $where[] =['a.id','eq',$product_id];
        if($type == 1){ //线上商家
            $warehouse_id = (new Warehouse())->findOne([['uid','eq',$uid]],"id");
            $where[] = ['b.warehouse_id','in',[$warehouse_id]];
            $stock = 0+(new Product())
                ->alias("a")
                ->join("om_warehouse_general_sku b","a.id = b.product_id","right")
                ->where($where)
                ->whereNull("b.delete_time")
                ->sum("b.stock");
        }if($type == 2){ //品牌店 自己的仓库和总仓库
            $warehouse_id = (new Warehouse())->findOne([['uid','eq',$uid]],"id");
            $where[] = ['b.warehouse_id','in',[$warehouse_id]];
            $stock = (new Product())
                ->alias("a")
                ->join("om_warehouse_general_sku b","a.id = b.product_id","left")
                ->where($where)
                ->whereNull("b.delete_time")
                ->sum("b.stock");
        }if($type == 3){ //贸易商
            $where[] = ['b.warehouse_id','in', 2];//贸易商在两个仓库 一个总仓库 一个中转仓
            $stock = (new Product())
                ->alias("a")
                ->join("om_warehouse_general_sku b","a.id = b.product_id","right")
                ->where($where)
                ->group("a.id")
                ->whereNull("b.delete_time")
                ->sum("b.stock");
        }

        return $stock;
    }

    /**
     * 判断商家或产品是否存在
     * @param $product_id
     * @return array
     */
    public function check_data($product_id){
        $where=[
            ['id','eq',$product_id],
            ['is_publish','eq',2]
        ];
        $list = $this->findOne($where,'*');
        if(!$list){
            return ['code'=>0,'msg'=>"产品不存在"];
        }
        $shop   =  (new Shop())->findOne([['id','eq',$list['uid']],['status','eq',2],['use_status','eq',1]],"id,name,type,phone");
        if(!$shop){
            return ['code'=>0,'msg'=>"商家不存在或者被封停"];
        }
        return ['code'=>1,'uid'=>$list['uid'],'list'=>$list,'shop'=>$shop];
    }

    public function getProductRule($list){
        //购物车商品对应的排序数组
        $productList = array_column($list,"product_id");
        $productList = array_unique($productList);
        $productRule = (new Product())->selectListWith("product_price",[['id','in',$productList]],"id,product_price_one");
        $productRule = $productRule->toArray();
        $productRule = changeArrayKey($productRule,"id");
        return $productRule;
    }


    //获取H5页面详情富文本页面
    public function getProductDescription($product_id){
//        return "http://114.116.75.81:8080/#/shopping-desc?id=".$product_id;
        return "http://admin.globaltc.cn/#/shopping-desc?id=".$product_id;
    }
}